package com.lemon.smartsafetynew.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.github.mikephil.charting.charts.HorizontalBarChart;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Andrew on 2017/8/8.
 */

public class MPChartScrollView extends ScrollView {


    private List<HorizontalBarChart> mHorizontalBarCharts = new LinkedList<>();

    public MPChartScrollView(Context context) {
        super(context);
    }

    public MPChartScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MPChartScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (changed) {
            getAllHorizontalBarChart(mHorizontalBarCharts, this); // 获得SwipeBackLayout中的ViewPager的集合
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (getTouchHorizontalBarChart(mHorizontalBarCharts, ev) != null) {
            return getTouchHorizontalBarChart(mHorizontalBarCharts, ev).dispatchTouchEvent(ev);
        }

        return super.onInterceptTouchEvent(ev);
    }

    private void getAllHorizontalBarChart(List<HorizontalBarChart> horizontalBarCharts, ViewGroup parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            if (child instanceof HorizontalBarChart) {
                horizontalBarCharts.add((HorizontalBarChart) child);
            } else if (child instanceof ViewGroup) {
                getAllHorizontalBarChart(horizontalBarCharts, (ViewGroup) child);
            }
        }
    }


    private HorizontalBarChart getTouchHorizontalBarChart(List<HorizontalBarChart> horizontalBarCharts,
                                                          MotionEvent ev) {
        if (horizontalBarCharts == null || horizontalBarCharts.size() == 0) {
            // 如果horizontalBarCharts集合为空，或者horizontalBarCharts的size=0，那么直接返回空
            return null;
        }
        Rect mRect = new Rect(); // 创建一个新的空矩形。所有坐标被初始化为0。
        for (HorizontalBarChart h : horizontalBarCharts) { // 遍历horizontalBarCharts集合，判断我现在触摸的地方是不是在某一个 HorizontalBarChart范围里
            // 如果在，那么就返回这个 viewpager
            h.getHitRect(mRect); // 获取每一个HorizontalBarChart的矩形的坐标值，并赋值给mRect矩形
            Log.e("yxy", "mRect.contains((int) ev.getX(), (int) ev.getY()) = " + mRect.contains((int) ev.getX(), (int) ev.getY()));
            if (mRect.contains((int) ev.getX(), (int) ev.getY())) {
                // 返回true，如果(x,y)坐标在mRect矩形的范围内
                return h; // 返回HorizontalBarChart
            }
        }
        return null; // 否则返回空
    }
}
